﻿using Model;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;

namespace TastsParallelTest
{
    public class Order
    {
        public int ID;
        public float Price;
    }

    public class OrderItem
    {
        public int ID;
        public string Goods;
        public int OrderID;
        public float Price;
        public int Count;
    }

    class Program
    {
        static void Main(string[] args)
        {
            ThreadPool.SetMaxThreads(1000, 1000);
            //并行查询
            //Parallel.For(0, 3, n =>
            //{
            //    Thread.Sleep(2000);
            //    ThreadPoolMessage(GetPersonList()[n]);
            //});
            //Parallel.ForEach(GetPersonList(), (person, state) =>
            //{
            //    if (person.ID == 2)
            //        state.Stop();
            //    ThreadPoolMessage(person);
            //});
            float totalPrice = 0f;
            var parallelResult = Parallel.ForEach(GetOrderList(),
                () => 0f,
                (order, state, orderPrice) =>
                {
                    orderPrice = GetOrderItem().Where(item => item.OrderID == order.ID)
                    .Sum(item => item.Price * item.Count);
                    order.Price = orderPrice;
                    ThreadPoolMessage(order);

                    return orderPrice;
                },
                (finallyPrice) =>
                {
                    totalPrice += finallyPrice;
                }
               );

            while (!parallelResult.IsCompleted)
                Console.WriteLine("Doing Work!");

            Console.WriteLine("Total Price is:" + totalPrice);
            Console.ReadKey();
        }

        static IList<Order> GetOrderList()
        {
            IList<Order> orderList = new List<Order>();
            Order order1 = new Order();
            order1.ID = 1;
            orderList.Add(order1);

            Order order2 = new Order();
            order2.ID = 2;
            orderList.Add(order2);

            Order order3 = new Order();
            order3.ID = 3;
            orderList.Add(order3);

            return orderList;
        }

        static IList<OrderItem> GetOrderItem()
        {
            IList<OrderItem> itemList = new List<OrderItem>();

            OrderItem orderItem1 = new OrderItem();
            orderItem1.ID = 1;
            orderItem1.Goods = "iPhone 8 plus";
            orderItem1.Price = 1000;
            orderItem1.Count = 5;
            orderItem1.OrderID = 1;
            itemList.Add(orderItem1);

            OrderItem orderItem2 = new OrderItem();
            orderItem2.ID = 2;
            orderItem2.Goods = "iPhone 8 plus";
            orderItem2.Price = 2000;
            orderItem2.Count = 5;
            orderItem2.OrderID = 1;
            itemList.Add(orderItem2);

            OrderItem orderItem3 = new OrderItem();
            orderItem3.ID = 3;
            orderItem3.Goods = "iPhone 8 plus";
            orderItem3.Price = 3000;
            orderItem3.Count = 5;
            orderItem3.OrderID = 1;
            itemList.Add(orderItem3);

            return itemList;
        }

        //显示线程池现状
        static void ThreadPoolMessage(Order order)
        {
            int a, b;
            ThreadPool.GetAvailableThreads(out a, out b);
            string message = string.Format("OrderID:{0}  OrderPrice:{1}\n" +
                  " CurrentThreadId is {2}\n WorkerThreads is:{3}  CompletionPortThreads is :{4}\n",
                  order.ID, order.Price, Thread.CurrentThread.ManagedThreadId, a.ToString(), b.ToString());

            Console.WriteLine(message);
        }
    }
}

namespace Model
{
    [Serializable]
    public class Person
    {
        public int ID { get; set; }
        public string Name { get; set; }
        public int Age { get; set; }
    }
}
